#include "kdcinfoplugin.h"

#include <KPluginFactory>
#include <KPluginLoader>

class KDCInfoPlugin;

#include <KLocale>

#include <kdclib.h>

K_PLUGIN_FACTORY( KDCInfoPluginFactory, registerPlugin< KDCInfoPlugin >(); )
K_EXPORT_PLUGIN( KDCInfoPluginFactory( "KDCInfoPlugin" ) )

KDCInfoPlugin::KDCInfoPlugin( QObject *parent, const QVariantList &args ) : KDCPluginInterface( parent ), pInfo( KPluginInfo("kdcinfoplugin.desktop", "services") ), pInterfaces( NULL ), pView( NULL ), pModel( NULL )
{
	Q_UNUSED( args );
}

KDCInfoPlugin::~KDCInfoPlugin()
{
	unload();
}

QString KDCInfoPlugin::interfaceName() const
{
	return "KDCInfoPlugin";
}

const KPluginInfo& KDCInfoPlugin::info() const
{
	return pInfo;
}

QWidget* KDCInfoPlugin::widget() const
{
	return pView;
}

KDCConfigPageInterface* KDCInfoPlugin::configPage() const
{
	return NULL;
}

KDC::Position KDCInfoPlugin::tabPosition() const
{
	return KDC::Right;
}

bool KDCInfoPlugin::load()
{
	if ( pView && pModel )
		return false;

	pView = new QTreeView;
	pView->setRootIsDecorated( true );
	pView->setUniformRowHeights( true );

	pModel = new KDCMovieInfoModel( this );
	pView->setModel( pModel );
	return true;
}

bool KDCInfoPlugin::unload()
{
	deletePtr( pModel );
	deletePtr( pView );
	return true;
}

bool KDCInfoPlugin::setInterfaces( QList<KDCBaseInterface*>& aInterfaces )
{
	pInterfaces = &aInterfaces;
	return true;
}

bool KDCInfoPlugin::init()
{
	foreach( KDCBaseInterface *interface, *pInterfaces )
		if ( interface->interfaceName() == "KDCDiscStructurePlugin" )
		{
			connect( static_cast<KDCPluginInterface*>( interface ), SIGNAL( objectChanged( KDCObject * ) ), this, SLOT( setObject( KDCObject * ) ) );
			return true;
		}
	return true;
}

KDCObject* KDCInfoPlugin::currentObject() const
{
	return NULL;
}

void KDCInfoPlugin::refresh()
{

}

void KDCInfoPlugin::setObject( KDCObject *aObject )
{
	if ( aObject && aObject->object() == KDC::Movie )
	{
		pModel->setMovie( static_cast<KDCMovie*>( aObject ) );
		pView->expandAll();
		return;
	}
	pModel->setMovie( NULL );
}


#include "kdcinfoplugin.moc"
